package com.ace.slide.service.impl;

import java.util.Properties;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

import com.ace.common.props.Props;
import com.ace.slide.enums.SlideShowBuilderPropsEnum;
import com.ace.slide.service.HibernateService;
import com.ace.slide.service.ShowBuilderContextService;

public class ShowBuilderContextServiceImpl implements ShowBuilderContextService
{
    private HibernateService hibernateService;

    public HibernateService getHibernateService()
    {
        return hibernateService;
    }

    public void wireServices() throws Exception
    {
        String propsFile = System.getProperty("propsFile", "slideshowbuilder.properties");
        Properties props = Props.loadProps(propsFile);
        wireServices(props);
    }

    public void wireServices(Properties props)
    {
        String hibernateConfigurationFile =
                props.getProperty(SlideShowBuilderPropsEnum.HibernateConfiguration.name());

        AnnotationConfiguration config = wireAnnotationConfiguration();
        config.configure(hibernateConfigurationFile);
        SessionFactory factory = config.buildSessionFactory();

        HibernateServiceImpl hibernateServiceImpl = new HibernateServiceImpl();
        hibernateServiceImpl.setFactory(factory);
        hibernateService = hibernateServiceImpl;
    }

    private AnnotationConfiguration wireAnnotationConfiguration()
    {
        AnnotationConfiguration config = new AnnotationConfiguration();
        config.addPackage("com.aceslideshows.slide.model");
        config.addAnnotatedClass(com.ace.slide.model.Address.class);
        config.addAnnotatedClass(com.ace.slide.model.Album.class);
        config.addAnnotatedClass(com.ace.slide.model.Media.class);
        config.addAnnotatedClass(com.ace.slide.model.Member.class);
        // config.addAnnotatedClass(com.aceslideshows.slide.model.Order.class);
        config.addAnnotatedClass(com.ace.slide.model.Profile.class);
        return config;

    }
}
